package org.groovyflow.spring.web;

import java.util.List;

import groovy.util.GroovyScriptEngine;
import groovy.lang.Binding;

/**
 * 
 */
class ControllerGroovyScriptEngineManager {
	private GroovyScriptEngine gse;
	private List dynamicRoots;
    private String mode;
    
    public void initGSE(){
    	if(isNonCompiledMode()){
	      	String[] roots =  (String[]) this.dynamicRoots.toArray(new String[this.dynamicRoots.size()]);
	      	try{
	      		gse  = new GroovyScriptEngine(roots);
	      	}catch(Exception ex){
				ex.printStackTrace();
				throw new RuntimeException(ex);	      		
	      	}
    	}
    }
   
    public boolean isNonCompiledMode(){
    	return mode.equals("dev");
    }

	public void setDynamicRoots(List dynamicRoots) {
		this.dynamicRoots = dynamicRoots;
	}
	public List getDynamicRoots() {
		return dynamicRoots;
	}	

	public void setMode(String mode) {
		this.mode = mode;
	}
	public String getMode() {
		return mode;
	}	
	
	public void run(String scriptName, Binding binding){
		try{
			this.gse.run(scriptName, binding);
		}catch(Exception ex){
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}



	
}
